use 	"./output/cities_expandedpatents", clear

keep if f_myr==tm(1918m9)
keep gen_id days_npis

tempfile days
save "`days'", replace

** GO **

use 	"./output/cities_expandedpatents", clear

** npi X month-of-year FEs **
egen	idxm = group(gen_id f_m)

** census region X year FEs **
egen	regyr = group(f_yr region)

set scheme plotplainblind
*=======================*
*= Treatment variables  *
*=======================*

tab 	days_npis if f_myr==tm(1918m9)

gen		daysnpis = days_npis/30 
gen		lnnpis = ln(days_npis)
gen		longnpis = (days_npis>=90) if !mi(days_npis) // Natural break in data, about 61% have short

foreach v of varlist daysnpis longnpis lnnpis {
	gen 	post_X_`v' 		= `v'*(f_myr>=tm(1918m9))

	gen 	during_X_`v' 	= `v'*( (f_myr>=tm(1918m9)) & (f_myr<=tm(1919m3)) )
	gen 	after_X_`v' 	= `v'*(f_myr>=tm(1919m4))
	gen 	pre_X_`v' 		= `v'*(f_myr<=tm(1917m8))
}

*=======================*
*= Main code  *
*=======================*

lab var post_X_daysnpis 	"(Days of NPIs)/30"
lab var post_X_lnnpis  		"Log Days of NPIS"
lab var post_X_longnpis 	"1[Days of NPIS>90]"

preserve 
	foreach v of varlist daysnpis {

		local vartext : variable label post_X_`v'

		matrix D = J(50,3,.)
		levelsof gen_id, local(dropone)
			
		foreach l of local dropone {
			
			tempfile cf_`l' 
			ppmlhdfe pat_wtd_inv post_X_`v' if tin(1916m1, 1920m12) & gen_id!=`l', exp(pop_int) a(f_myr idxm regyr) cluster(gen_id)
			parmest, sa("cf_`l'", replace) idnum(`l')

		}
		clear
		
		foreach l of local dropone {
			append using "cf_`l'"
		}
		
		keep if parm=="post_X_`v'"
		rename idnum gen_id
		merge 1:1 gen_id using "`days'"
		drop _merge
		
		gen 	pcat = 1 if p<=0.01
		replace pcat = 2 if p>0.01 & p<=0.05
		replace pcat = 3 if p>0.05 & p<=0.10
		replace pcat = 4 if p>0.10 & !mi(p)
		
		sum estimate p, d
		
		tempfile g1
		twoway (scatter estimate days_npis if pcat==1, m(o)) || ///
			(scatter estimate days_npis if pcat==2, m(oh)) || ///
			(scatter estimate days_npis if pcat==3, m(+)) || ///
			(scatter estimate days_npis if pcat==4, m(t) mcolor(orange)), ///
			xtitle("Days of NPIs for Omitted City", size(small)) ytitle("Leave-one-out coef. on `vartext'", size(small)) ///
			ysc(r(0)) xlabel(, nogrid) ylabel(#10, nogrid) ///
			legend(pos(6) row(1) order(1 "p<0.01" 2 "p<0.05" 3 "p<0.10" 4 "p>=0.10")) ///
			saving("`g1'", replace)
			
		egen jk_mean = mean(estimate)
		gen	 jk_deviate = (estimate-jk_mean)^2
		egen jk_ssqe = sum(jk_deviate)
		gen	 jk_se = sqrt((50/(50-1)) * jk_ssqe)
		gen	 jk_t = jk_mean / jk_se
		
		sum jk_mean
		local	jk_cont_m = `r(mean)'
		sum jk_se
		local	jk_cont_se = `r(mean)'
		sum jk_t
		local	jk_cont_t = `r(mean)'
		
	}
restore


preserve 
	foreach v of varlist longnpis {

		local vartext : variable label post_X_`v'

		matrix D = J(50,3,.)
		levelsof gen_id, local(dropone)
			
		foreach l of local dropone {
			
			tempfile cf_`l' 
			ppmlhdfe pat_wtd_inv post_X_`v' if tin(1916m1, 1920m12) & gen_id!=`l', exp(pop_int) a(f_myr idxm regyr) cluster(gen_id)
			parmest, sa("cf_`l'", replace) idnum(`l')

		}
		clear
		
		foreach l of local dropone {
			append using "cf_`l'"
		}
		
		keep if parm=="post_X_`v'"
		rename idnum gen_id
		merge 1:1 gen_id using "`days'"
		drop _merge
		
		gen 	pcat = 1 if p<=0.01
		replace pcat = 2 if p>0.01 & p<=0.05
		replace pcat = 3 if p>0.05 & p<=0.10
		replace pcat = 4 if p>0.10 & !mi(p)
		
		sum estimate p, d
			
		tempfile g2
		twoway (scatter estimate days_npis if pcat==1, m(o)) || ///
			(scatter estimate days_npis if pcat==2, m(oh)) || ///
			(scatter estimate days_npis if pcat==3, m(+)) || ///
			(scatter estimate days_npis if pcat==4, m(t) mcolor(orange)), ///
			xtitle("") ytitle("Leave-one-out coef. on `vartext'", size(small)) ///
			ysc(r(0)) xlabel(, nogrid) ylabel(#10, nogrid) ///
			legend(off) ///
			saving("`g2'", replace)
			
		egen jk_mean = mean(estimate)
		gen	 jk_deviate = (estimate-jk_mean)^2
		egen jk_ssqe = sum(jk_deviate)
		gen	 jk_se = sqrt((50/(50-1)) * jk_ssqe)
		gen	 jk_t = jk_mean / jk_se
				
		sum jk_mean
		local	jk_bin_m = `r(mean)'
		sum jk_se
		local	jk_bin_se = `r(mean)'
		sum jk_t
		local	jk_bin_t = `r(mean)'
	}
restore

graph combine "`g2'" "`g1'", c(1) ysize(6) xsize(5) imargin(medsmall) 
graph export "$RES/FigureAX_leaveoneout.png", replace	
	
di `jk_cont_m'	
di `jk_cont_se'	
di `jk_cont_t'	
di `jk_bin_m'	
di `jk_bin_se'	
di `jk_bin_t'	
	
est clear

foreach l of local dropone {
	rm cf_`l'.dta
}


/*	
St Paul, MN	28
Detroit, MI	29
Newark, NJ	33
New Haven, CT	39
Syracuse, NY	39
Dallas, TX	41
Providence, RI	42
Baltimore, MD	43
Worcester, MA	44
Atlanta, GA	46
Albany, NY	47
Birmingham, AL	48
Buffalo, NY	49
Cambridge, MA	49
Boston, MA	50
Philadelphia, PA	51
Pittsburgh, PA	53
Rochester, NY	54
Nashville, TN	55
Des Moines, IA	56
Lowell, MA	59
Fall River, MA	60
Richmond, VA	60
Grand Rapids, MI	62
Washington, DC	64
San Francisco, CA	67
Chicago, IL	68
Charleston, SC	69
New York City, NY	73
New Orleans, LA	78
San Antonio, TX	81
Indianapolis, IN	82
Cleveland, OH	99
Toledo, OH	102
Minneapolis, MN	116
Cincinnati, OH	123
Oakland, CA	127
Milwaukee, WI	132
Omaha, NE	140
Salt Lake City, UT	141
St Louis, MO	143
Louisville, KY	145
Columbus, OH	147
Denver, CO	151
Los Angeles, CA	154
Dayton, OH	156
Portland, OR	162
Spokane, WA	164
Seattle, WA	168
Kansas City, MO	170

